package birdry.display
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;


	/**
	 * 점선 클래스  
	 * @author B
	 * <br>
	 * 두 점을 잇는 점선 생성
	 * 점선의 굵기,색상,간격,점의 길이 설정 가능
	 */	
	public class DotLine extends Sprite implements ILine
	{
		
		private var _lineColor:uint 	  = 0x777777;
		private var _lineAlpha:Number	  = 1;
		private var _lineThickness:uint	  = 1;
		private var _dotLength:uint		  = 3;
		private var _spacing:Number		  = 1;
		private var _width:Number;
		private var _height:Number;
		
		private var _point1:Point;
		private var _point2:Point;
		
		
		public function DotLine( point1:Point, point2:Point, lineThickness:uint, lineColor:uint, dotLength:uint, lineAlpha:Number=1)		{
			super();
			_lineThickness = lineThickness;
			_lineColor = lineColor;
			_dotLength = dotLength;
			_lineAlpha = lineAlpha;
			draw(point1, point2);
		}		
		
		// 점선 그리기
		public function draw(point1:Point, point2:Point):void
		{
			_point1 = point1;
			_point2 = point2;
			var patternLength:Number	= _dotLength + _dotLength * _spacing ;
			var ratio:Number			= _dotLength / patternLength ;
			var p1:uint					= int( 255 * ratio ) ;
			var p2:uint					= p1 + 1;	
			var matrix:Matrix			= new Matrix();
			var rotate:Number			= Math.atan2( point2.y - point1.y, point2.x - point1.x );
			
			matrix.createGradientBox( patternLength, patternLength, rotate );
			
			var g:Graphics = this.graphics;
			g.clear();
			g.lineStyle( _lineThickness, _lineColor, 0, false, "normal", "none" );
			g.lineGradientStyle( "linear", [ _lineColor, _lineColor, 0x000000, 0x000000 ], [ _lineAlpha*100, _lineAlpha*100, 0, 0 ], [ 0, p1, p2, 255 ], matrix, "repeat", "linearRGB", 0 );
			g.moveTo( point1.x, point1.y );
			g.lineTo( point2.x, point2.y );
		}		
		
		/**
		 * 점의 색상
		 * @param value
		 */
		public function set lineColor( value:uint ): void
		{
			_lineColor = value;
			draw(_point1, _point2);
		}
		public function get lineColor( ):uint
		{
			return _lineColor;
		}
		
		/**
		 * 선의 투명도 
		 * @param value : 0 ~ 1
		 */		
		public function set lineAlpha( value:Number ):void
		{
			_lineAlpha = value;
			draw(_point1, _point2);
		}
		public function get lineAlpha( ):Number
		{
			return _lineAlpha;
		}
		
		/**
		 * 선의 두께 
		 * @param value
		 */		
		public function set lineThickness( value:uint ):void
		{
			_lineThickness = value;
			draw(_point1, _point2);
		}
		public function get lineThickness( ):uint
		{
			return _lineThickness;
		}
		
		/**
		 * 점의 길이
		 * @param value
		 */		
		public function set dotLength( value:uint ):void
		{
			_dotLength = value;
			draw(_point1, _point2);
		}
		public function get dotLength():uint
		{
			return _dotLength;
		}
		
		/**
		 * 점 길이에 대한 공백 길이의 비율 
		 * @param value	: 0 ~ 1
		 * <br>
		 * value=1 이면 점과 공백의 길이비율 1:1
		 */		
		public function set spacing( value:Number ):void
		{
			_spacing = value;
			draw(_point1, _point2);
		}
		public function get spacing():Number
		{
			return _spacing;
		}
		
		/**
		 * width 재정의 - 가로길이 조절시에도 기존패턴 유지 
		 * @param value
		 */
		override public function set width( value:Number ):void
		{
			_width = value;
			_point2.x = _point1.x + _width;
			draw(_point1, _point2);
		}
		override public function get width():Number
		{
			return _width;
		}
		
		/**
		 * height 재정의 - 세로길이 조절시에도 기존패턴 유지 
		 * @param value
		 */		
		override public function set height(value:Number):void
		{
			_height = value;
			_point2.y = _point1.y + _height;
			draw(_point1, _point2);
		}
		override public function get height():Number
		{
			return _height;
		}
		
		
	}
}